package com.yunhan.security;

import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description
 * @Author liuzhenkun
 * @Date 2024/12/21 15:53
 */
public interface SecurityUrl {

    //服务基础路径
    String BASE_URL = "/";


    /**
     * 是否在白名单内
     * @param requestURI
     * @return
     */
    static Boolean isMatchWhiteUrl(String requestURI) {
        PathMatcher pathMatcher = new AntPathMatcher();
        for (String url : RESOURCE.URL_WHITELIST) {
            if (pathMatcher.match( url, requestURI)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 是否在静态资源内
     * @param requestURI
     * @return
     */
    static Boolean isMatchStatic(String requestURI) {
        PathMatcher pathMatcher = new AntPathMatcher();
        for (String url : RESOURCE.STATIC) {
            if (pathMatcher.match( url, requestURI)) {
                return true;
            }
        }
        return false;
    }


    class RESOURCE {
        //所有需要放行的资源
        public static List<String> URL_WHITELIST;

        //定义白名单，无需token
        public static List<String> SAFE_URL = Arrays.asList(
                BASE_URL + "/login",
                BASE_URL + "/error/**");

        //定义静态资源
        public static List<String> STATIC = Arrays.asList(
                // -- swagger ui
                BASE_URL + "/favicon.ico",
                BASE_URL + "/swagger-ui.html",
                BASE_URL + "/webjars/**",
                BASE_URL + "/swagger-resources/",
                BASE_URL + "/swagger-resources/**",
                BASE_URL + "/v2/api-docs-ext/**",
                BASE_URL + "/v2/api-docs",
                BASE_URL + "/v2/**",
                BASE_URL + "/error/**",
                BASE_URL + "/mode-Text.js",
                BASE_URL + "/ext-searchbox.js",
                BASE_URL + "/doc.html");



        static {
            //初始化所有需要放行的资源
            URL_WHITELIST = new ArrayList<>();
            URL_WHITELIST.addAll(SAFE_URL);
            URL_WHITELIST.addAll(STATIC);
        }



    }
}
